<?php
/**
 * CreateTime: 2022/5/1 16:35
 */

namespace App\ServiceChecker\Client;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Models\Client\ClientCreditTableModel;
use App\Models\Client\ClientModel;
use App\Models\Client\CreditClientModel;
use App\Models\Department\DepartmentModel;
use App\ServiceChecker\BasicServiceChecker;
use Illuminate\Http\Exceptions\HttpResponseException;

class ClientCreditLegitimacy extends BasicServiceChecker
{
    public ClientCreditTableModel $clientCreditTableModel;
    public DepartmentModel $departmentModel;
    public ClientModel $clientModel;
    public CreditClientModel $creditClientModel;

    public function __construct()
    {
        parent::__construct();
        $this->clientCreditTableModel = new ClientCreditTableModel();
        $this->departmentModel = new DepartmentModel();
        $this->clientModel = new ClientModel();
        $this->creditClientModel = new CreditClientModel();
    }


    /**
     * 检测部门是否存在
     * @param $departId
     */
    public function verifyDepartmentIsExists($departId){
        $isExist = $this->departmentModel->isExistByCondition([
            'department_id' => $departId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "部门不存在",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    /**
     * 检测客户是否存在
     * @param $clientId
     */
    public function verifyClientIsExists($clientId){
        $isExist = $this->clientModel->isExistByCondition([
            'client_id' => $clientId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "客户不存在",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    /**
     * 检测信用评价表是否存在
     * @param $clientCreditTableId
     */
    public function verifyClientCreditTableIsExists($clientCreditTableId){
        $isExist = $this->clientCreditTableModel->isExistByCondition([
            'client_credit_table_id' => $clientCreditTableId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "客户信用表单不存在",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    public function verifyClientCreditSendProcessIsOK($clientId){
       $res =  $this->clientModel->getClientInfoByClientId($clientId,[
           'client_status'
       ]);
       if($res['client_status']!=2 && $res['client_status']!=5 && $res['client_status']!=8){
           throw new HttpResponseException( ResponseFunc::returnJson(
               'error',
               null,
               "当前信用客户已送审或送审已通过",
               ResponseStatusConsts::OK_200_RESPONSE_STATUS,
           ));
       }
    }

    public function verifyClientCreditIdClientIsExists($clientId)
    {
        $isExist = $this->creditClientModel->isExistByCondition([
            'client_id' => $clientId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "信用客户不存在",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }


    public function verifyClientCreditIdIsExists($clientCreditId){
        $isExist = $this->creditClientModel->isExistByCondition([
            'credit_client_id' => $clientCreditId,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "信用客户不存在",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    public function verifyClientCreditIdBlackIsExists($clientCreditId){
        $isExist = $this->creditClientModel->isExistByCondition([
            'credit_client_id' => $clientCreditId,
            'is_black_credit_client' => 2,
            'status' => BaseConsts::STATUS_NORMAL,
            'audit_status' => 1
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前客户不能送审至黑名单",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

    public function verifyClientCreditIdBlackIsOkExists($clientCreditId){
        $isExist = $this->creditClientModel->isExistByCondition([
            'credit_client_id' => $clientCreditId,
            'is_black_credit_client' => 1,
            'audit_status' => 1,
            'status' => BaseConsts::STATUS_NORMAL
        ]);
        if(!$isExist){
            throw new HttpResponseException( ResponseFunc::returnJson(
                'error',
                null,
                "当前客户未加入黑名单",
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            ));
        }
    }

}